package com.lyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyun.mapper.CommentLikeMapper;
import com.lyun.pojo.CommentLike;
import com.lyun.service.CommentLikeService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 动态的评论点赞
 */
@DubboService
public class CommentLikeServiceImpl extends ServiceImpl<CommentLikeMapper, CommentLike> implements CommentLikeService {

    @Resource
    private CommentLikeMapper commentLikeMapper;

    @Override
    public int getCommentsLikeCount(Long commentId) {
        QueryWrapper<CommentLike> queryWrapper = new QueryWrapper<>();
        //根据传过来的 评论commentId 就可以算出有几条关于他的点赞信息
        queryWrapper.eq("comment_id", commentId);
        queryWrapper.eq("is_delete", 0);
        return commentLikeMapper.selectCount(queryWrapper);
    }

    @Override
    public int getIsLikeByUserIdAndCommentId(Long userId, Long commentId) {
        QueryWrapper<CommentLike> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("comment_id", commentId);
        CommentLike commentLike = commentLikeMapper.selectOne(queryWrapper);

        if (commentLike != null) {
            //不为空就说明有这条记录 说明点过赞 判断一下是否逻辑删除了isdelete
            if (commentLike.getIsDelete() == 0) {
                return 1;
            }
        }

        return 0;
    }

    /**
     * 给评论点赞
     *
     * @param commentId
     * @param userId
     * @return 评论的点赞数
     */
    @Override
    public int likeComment(Long commentId, Long userId) {

        QueryWrapper<CommentLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("comment_id", commentId);

        CommentLike selectCommentLike = commentLikeMapper.selectOne(queryWrapper);

        if (Objects.isNull(selectCommentLike)) {
            //如果为空就插入一个
            CommentLike commentLike = new CommentLike();
            commentLike.setUserId(userId);
            commentLike.setCommentId(commentId);
            commentLike.setTime(LocalDateTime.now());
            commentLike.setIsDelete(0);
            this.save(commentLike);
        } else {
            //不为空就更新一下逻辑删除字段 然后保存
            selectCommentLike.setIsDelete(0);
            this.updateById(selectCommentLike);
        }

        queryWrapper.clear();
        queryWrapper.eq("comment_id", commentId);
        queryWrapper.eq("is_delete", 0);

        return commentLikeMapper.selectCount(queryWrapper);
    }

    @Override
    public int dislikeComment(Long commentId, Long userId) {

        QueryWrapper<CommentLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("comment_id", commentId);

        CommentLike selectCommentLike = commentLikeMapper.selectOne(queryWrapper);
        if (Objects.nonNull(selectCommentLike)) {
            selectCommentLike.setIsDelete(1);
            commentLikeMapper.updateById(selectCommentLike);
        }

        queryWrapper.clear();
        queryWrapper.eq("comment_id", commentId);
        queryWrapper.eq("is_delete", 0);

        return commentLikeMapper.selectCount(queryWrapper);
    }

}
